import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-09-28
 * Time: 19:07
 */
class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode(int val) {
        this.val = val;
    }
}
public class Day8 {
    //236.二叉树的最近公共祖先
    /*public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) return null;

        if (root == p || root == q){
            return root;
        }
        TreeNode leftTree = lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree = lowestCommonAncestor(root.right,p,q);

        if (leftTree != null && rightTree != null){
            return root;
        }else if (leftTree != null){
            return leftTree;
        }else{
            return rightTree;
        }
    }*/

    //双栈：
    public boolean getRows(TreeNode root, TreeNode p, Stack<TreeNode> stack){
        if (root == null) return false;
        stack.push(root);

        if (root == p){
            return true;
        }
        boolean flg = getRows(root.left, p,stack);
        if (flg){
            return true;
        }

        flg = getRows(root.right, p, stack);
        if (flg){
            return true;
        }

        stack.pop();
        return false;
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) return null;

        Stack<TreeNode> stack1 = new Stack<>();
        getRows(root, p, stack1);
        Stack<TreeNode> stack2 = new Stack<>();
        getRows(root, q, stack2);

        int size1 = stack1.size();
        int size2 = stack2.size();
        if (size1 > size2){
            int size = size1 - size2;
            while (size > 0){
                stack1.pop();
                size--;
            }
        }else{
            int size = size2 - size1;
            while (size > 0){
                stack2.pop();
                size--;
            }
        }

        while (!stack1.empty() && !stack2.empty()){
            if (stack1.peek() == stack2.peek()){
                return stack1.pop();
            }else{
                stack1.pop();
                stack2.pop();
            }
        }
        return null;
    }

    //105.从前序与中序遍历序列构造二叉树
    /*public int preIndex = 0;
    public TreeNode createTreePandI(int[] preprder, int[] inorder, int inbegin, int inend){
        if (inbegin > inend){
            return null;
        }

        TreeNode root = new TreeNode(preprder[preIndex]);

        int rootindex = findRootindex(inorder,inbegin,inend,preprder[preIndex]);
        if (rootindex == -1){
            return null;
        }
        preIndex++;
        root.left = createTreePandI(preprder,inorder,inbegin,rootindex - 1);
        root.right = createTreePandI(preprder,inorder,rootindex + 1, inend);

        return root;
    }
    public int findRootindex(int[] inorder, int inbegin, int inend, int key){
        for (int i = inbegin; i <= inend; i++) {
            if (inorder[i] == key){
                return i;
            }
        }
        return -1;
    }
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if (preorder == null || inorder == null) return null;

        return createTreePandI(preorder,inorder,0, inorder.length - 1);
    }*/

    //106.从中序与后序遍历构造二叉树
    public int postIndex = 0;//回溯过程会改变其值
    public TreeNode createTreeByPandI(int[] inorder, int[] postorder,int inbegin, int inend){
        if (inbegin > inend){
            return null;
        }
        TreeNode root = new TreeNode(postorder[postIndex]);
        int rootIndex = findIndexOfI(inorder,inbegin,inend,postorder[postIndex]);
        if (rootIndex == -1){
            return null;
        }
        postIndex--;
        root.right = createTreeByPandI(inorder, postorder,rootIndex + 1,inend);
        root.left = createTreeByPandI(inorder,postorder, inbegin, rootIndex - 1);
        return root;
    }
    private int findIndexOfI(int[] inorder,int inbegin, int inend, int key){
        for (int i = inbegin; i <= inend; i++) {
            if (inorder[i] == key){
                return i;
            }
        }
        return -1;
    }

    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if (postorder == null || inorder == null) return  null;
        postIndex = postorder.length - 1;

        return createTreeByPandI(inorder,postorder,0,inorder.length - 1);
    }

    //606.根据二叉树创建字符串
    public void treeToString(TreeNode root, StringBuilder sb){
        if (root == null) return;
        sb.append(root.val);

        if (root.left != null){
            sb.append("(");
            treeToString(root.left, sb);
            sb.append(")");
        }else{
            if (root.right == null){
                return;
            }else{
                sb.append("()");
            }
        }

        if (root.right != null){
            sb.append("(");
            treeToString(root.right,sb);
            sb.append(")");
        }else{
            return;
        }
    }
    public String tree2str(TreeNode root) {
        if (root == null){
            return null;
        }
        StringBuilder sb = new StringBuilder();
        treeToString(root, sb);
        return sb.toString();
    }

    //107.二叉树的层序遍历II - 标记
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        LinkedList<List<Integer>> lists = new LinkedList<>();
        if (root == null) return lists;

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            while(size != 0) {
                TreeNode node = queue.poll();
                list.add(node.val);
                //System.out.println(node.val);
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                size--;
            }
            lists.addFirst(list);
        }
        return lists;
    }

    //108.将有序数组转换为二叉搜索树 - 标记
    public TreeNode getBST(int[] nums, int inbegin, int inend){
        if (inbegin > inend){
            return null;
        }
        int rootIndex = 0;
        if (inend % 2 == 0){
            rootIndex = inend / 2;
        }else{
            rootIndex = inend / 2 + 1;
        }
        TreeNode root = new TreeNode(nums[rootIndex]);

        root.left = getBST(nums, inbegin, rootIndex - 1);
        root.right = getBST(nums,rootIndex + 1, inend);

        return root;
    }

    public TreeNode sortedArrayToBST(int[] nums) {
        if (nums.length == 0){
            return null;
        }
        return getBST(nums,0,nums.length);
    }
}
